'\" te
.\" Copyright 1989 AT&T  Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH PTRACE 3C "Mar 22, 2004"
.SH NAME
ptrace \- allows a parent process to control the execution of a child process
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>
#include <sys/types.h>

\fBint\fR \fBptrace\fR(\fBint\fR \fIrequest\fR, \fBpid_t\fR \fIpid\fR, \fBint\fR \fIaddr\fR, \fBint\fR \fIdata\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBptrace()\fR function allows a parent process to control the execution of
a child process. Its primary use is for the implementation of breakpoint
debugging. The child process behaves normally until it encounters a signal (see
\fBsignal.h\fR(3HEAD)), at which time it enters a stopped state and its parent
is notified by the \fBwait\fR(3C) function. When the child is in the stopped
state, its parent can examine and modify its "core image" using \fBptrace()\fR.
Also, the parent can cause the child either to terminate or continue, with the
possibility of ignoring the signal that caused it to stop.
.sp
.LP
The \fIrequest\fR argument determines the action to be taken by \fBptrace()\fR
and is one of the following:
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 5n
This request must be issued by the child process if it is to be traced by its
parent. It turns on the child's trace flag that stipulates that the child
should be left in a stopped state on receipt of a signal rather than the state
specified by \fIfunc\fR (see \fBsignal\fR(3C)). The \fIpid\fR, \fIaddr\fR, and
\fIdata\fR arguments are ignored, and a return value is not defined for this
request. Peculiar results ensue if the parent does not expect to trace the
child.
.RE

.sp
.LP
The remainder of the requests can only be used by the parent process. For each,
\fIpid\fR is the process \fBID\fR of the child. The child must be in a stopped
state before these requests are made.
.sp
.ne 2
.na
\fB\fB1, 2\fR\fR
.ad
.RS 8n
With these requests, the word at location \fIaddr\fR in the address space of
the child is returned to the parent process. If instruction and data space are
separated, request \fB1\fR returns a word from instruction space, and request
\fB2\fR returns a word from data space. If instruction and data space are not
separated, either request \fB1\fR or request \fB2\fR may be used with equal
results. The \fIdata\fR argument is ignored. These two requests fail if
\fIaddr\fR is not the start address of a word, in which case \fB\(mi1\fR is
returned to the parent process and the parent's \fBerrno\fR is set to
\fBEIO\fR.
.RE

.sp
.ne 2
.na
\fB\fB3\fR\fR
.ad
.RS 8n
With this request, the word at location \fIaddr\fR in the child's user area in
the system's address space (see <\fBsys/user.h\fR>) is returned to the parent
process. The \fIdata\fR argument is ignored. This request fails if \fIaddr\fR
is not the start address of a word or is outside the user area, in which case
\fB\(mi1\fR is returned to the parent process and the parent's \fBerrno\fR is
set to \fBEIO\fR.
.RE

.sp
.ne 2
.na
\fB\fB4, 5\fR\fR
.ad
.RS 8n
With these requests, the value given by the \fIdata\fR argument is written into
the address space of the child at location \fIaddr\fR. If instruction and data
space are separated, request \fB4\fR writes a word into instruction space, and
request \fB5\fR writes a word into data space. If instruction and data space
are not separated, either request \fB4\fR or request \fB5\fR may be used with
equal results. On success, the value written into the address space of the
child is returned to the parent. These two requests fail if \fIaddr\fR is not
the start address of a word. On failure \fB\(mi1\fR is returned to the parent
process and the parent's \fBerrno\fR is set to \fBEIO\fR.
.RE

.sp
.ne 2
.na
\fB\fB6\fR\fR
.ad
.RS 8n
With this request, a few entries in the child's user area can be written.
\fIdata\fR gives the value that is to be written and \fIaddr\fR is the location
of the entry. The few entries that can be written are the general registers and
the condition codes of the Processor Status Word.
.RE

.sp
.ne 2
.na
\fB\fB7\fR\fR
.ad
.RS 8n
This request causes the child to resume execution. If the \fIdata\fR argument
is 0, all pending signals including the one that caused the child to stop are
canceled before it resumes execution. If the \fIdata\fR argument is a valid
signal number, the child resumes execution as if it had incurred that signal,
and any other pending signals are canceled. The \fIaddr\fR argument must be
equal to 1 for this request. On success, the  value of \fIdata\fR is returned
to the parent. This request fails if \fIdata\fR is not 0 or a valid signal
number, in which case \fB\(mi1\fR is returned to the parent process and the
parent's \fBerrno\fR is set to \fBEIO\fR.
.RE

.sp
.ne 2
.na
\fB\fB8\fR\fR
.ad
.RS 8n
This request causes the child to terminate with the same consequences as
\fBexit\fR(2).
.RE

.sp
.ne 2
.na
\fB\fB9\fR\fR
.ad
.RS 8n
This request sets the trace bit in the Processor Status Word of the child and
then executes the same steps as listed above for request \fB7\fR. The trace bit
causes an interrupt on completion of one machine instruction. This effectively
allows single stepping of the child.
.RE

.sp
.LP
To forestall possible fraud, \fBptrace()\fR inhibits the set-user-ID facility
on subsequent calls to one of the \fBexec\fR family of functions (see
\fBexec\fR(2)). If a traced process calls one of these functions, it stops
before executing the first instruction of the new image showing signal
\fBSIGTRAP\fR.
.SH ERRORS
.sp
.LP
The \fBptrace()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 9n
The \fIrequest\fR argument is an illegal number.
.RE

.sp
.ne 2
.na
\fB\fBEPERM\fR\fR
.ad
.RS 9n
The calling process does not have appropriate privileges to control the calling
process. See \fBproc\fR(5).
.RE

.sp
.ne 2
.na
\fB\fBESRCH\fR\fR
.ad
.RS 9n
The \fIpid\fR argument identifies a child that does not exist or has not
executed a \fBptrace()\fR call with request \fB0\fR.
.RE

.SH USAGE
.sp
.LP
The \fBptrace()\fR function is available only with the 32-bit version of
\fBlibc\fR(3LIB). It is not available with the 64-bit version of this library.
.sp
.LP
The \fB/proc\fR debugging interfaces should be used instead of \fBptrace()\fR,
which provides quite limited debugger support and is itself implemented using
the \fB/proc\fR interfaces. There is no actual \fBptrace()\fR system call in
the kernel. See \fBproc\fR(5) for descriptions of the \fB/proc\fR debugging
interfaces.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
.BR exec (2),
.BR exit (2),
.BR signal (3C),
.BR wait (3C),
.BR signal.h (3HEAD),
.BR libc (3LIB),
.BR proc (5),
.BR attributes (7)
